package 数据库测试;

import java.sql.*;

/**
 * @作者:侯国威
 * @时间: 2020/4/11/12:29
 * @邮箱:1045077189@qq.com
 */
public class jdbc_test {
    private static  final String DIRVER_NAME = "com.mysql.cj.jdbc.Driver";
    private static final String URL = "jdbc:mysql://localhost:3306/stu";
    private static final String USER_NAME = "root";
    //密码
    private static final String PASSWORD = "1641112";
    public static void main(String[] args) {
            Connection connection=null;
            try {
                //加载驱动

                Class.forName(DIRVER_NAME);
                connection= DriverManager.getConnection(URL,USER_NAME,PASSWORD);

                String sql="SELECT id,password,user FROM student";
               PreparedStatement preparedStatement=connection.prepareStatement(sql);
               ResultSet resultSet = preparedStatement.executeQuery();
               System.out.println("id"+"\t"+"user"+"\t"+"password");
               while (resultSet.next()){

                   System.out.println(resultSet.getString("id")+"\t"+resultSet.getString("user")+"\t"+resultSet.getString("password"));

               }
               resultSet.close();
               preparedStatement.close();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (SQLException e) {
                e.printStackTrace();
            }finally {
                if (connection!=null){
                    try {
                        connection.close();
                    }catch (SQLException e){
                        e.printStackTrace();
                    }
                }
            }
    }
}
